﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Mls.IO
{
    public static class DirectoryChainFinder
    {
        public static DocumentsChain[] Find(string path, out string[] errorMessages, Language.LanguageSettings settings)
        {
            List<Document> documents = new List<Document>();
            List<string> errors = new List<string>();
            List<DocumentsChain> chains = new List<DocumentsChain>();

            foreach (string filename in Directory.GetFiles(path))
            {
                if (Path.GetExtension(filename).Length > 0)
                    continue;

                try
                {
                    documents.Add(new Document(filename, settings));
                }
                catch (Exception exception)
                {
                    errors.Add(string.Format("Error loading file {0}: {1}", filename, exception.Message));
                }
            }

            while (documents.Count > 0)
                chains.Add(DocumentsChain.ConsumeChain(documents, errors));

            errorMessages = errors.ToArray();
            return chains.ToArray();
        }
    }
}
